/*
 *  Copyright (C) 2022  贺龙宇
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#include <opencv2/opencv.hpp>
#include "../define.h"
#include "../SerialPort/Protocol.h"
#include "../Config/CameraParam.h"
#include "../Config/RuneParam.h"




class RuneDetector{
public:
    RuneDetector();
    bool DetectRune(const cv::Mat &src, cv::Point3f &target);
private:

    /*参数统一移植到Config/RumeParam.yml中，方便调试*/

	/*一些识别之外的常数*/
    // double rad;
    // double pi2;
    // double frame_interval_s ; //帧间隔，单位s
    int test_spd_frame;
    // double predict_time ; //预测时间，单位s
    // double arm_r; //旋臂半径，单位px（目前采用实时测量法）


	// /*与识别有关的常数*/
    // double r_max_ratio;/*R标的长宽最大比例（即超过这个值就认为不是R标）*/
    // double r_min_area;/*R标最小面积*/
	// double r_max_area;/*R标最大面积*/
	// double leaf_min_area; /*扇叶最小面积*/
	// double leaf_max_area; /*扇叶最大面积*/
	// double leaf_min_ratio; /*扇叶最小比例*/
	// double leaf_max_ratio;  /*扇叶最大比例*/
	// double armor_min_ratio; /*装甲板最小比例*/
	// double armor_max_ratio; /*装甲板最大比例*/
	// double armor_min_area;
	// double armor_max_area;

    // double R2armor_min_dist; /*R标到击打目标的最小距离要求*/
    // double R2armor_max_dist; /*R标到击打目标的最大距离要求*/

	// int blue_thresh; /*识别蓝色时的阈值*/
    // int red_thresh;/*识别红色时的阈值*/
	// int blue_color_thresh;
	// int red_color_thresh;
    
    /*以下这些部分暂时无用，为最初的大符设计方案*/
    
	bool R_tag_found;/*是否找到R标*/
	bool armor_found;/*是否找到待击打装甲板*/
    bool init_flag;/*大符初始化标志*/
    bool isLost; /*判断上一时刻是否掉帧*/
    
    // float a; /*大符参数*/
    // float omega;    /*大符参数*/
    // float b;/*大符参数*/
    // float phase;/*大符参数*/
    // float current_t;/*大符参数*/

    void GetTarget(const cv::Mat &src, cv::RotatedRect &target_rect, cv::RotatedRect &R_tag);/*识别R标和击打装甲板*/
    void Init(const cv::RotatedRect &R_tag, cv::RotatedRect &target_rect);/*初始化，暂时无用*/
    void SolveAndPredict_SMALL(const cv::RotatedRect &target_rect, const cv::RotatedRect &R_tag, cv::RotatedRect &predict_rect);/*预测击打目标*/
    void SolveAndPredict_BIG(const cv::RotatedRect &target_rect, const cv::RotatedRect &R_tag, cv::RotatedRect &predict_rect);/*预测击打目标*/
    cv::RotatedRect Estimation(const cv::RotatedRect &R_tag, const cv::RotatedRect &predict_rect);/*弹道补偿*/
    void SolvePosition(const cv::Point2f &aim_point,cv::Point3f &target);/*位置解算*/

    


    cv::Mat show_contours; /*DEBUG 用*/
    cv::Mat show_area;  /*DEBUG 用*/
    cv::Mat show_final; /*DEBUG 用*/
    cv::Mat show_ratio; /*DEBUG 用*/
    cv::Mat show_inside_count;  /*DEBUG 用*/
    cv::Mat show_graph;/*DEBUG 用*/

    

};

/*调试使用，用于画图*/
void Plot(cv::Mat &plot_mat, double max_x, double max_y, double x, double y,const cv::Scalar &scalar,const std::string &name="");